// 页面路径：store/index.js
import { createStore } from 'vuex'
const store = createStore({
	state:{//存放状态
		currentPage: '/pages/index/index',// 当前页
		userInfo:null,// 用户信息
		isLoginOut:false,// 登录状态
		eventFold: false,// 导航栏折叠
	},
	mutations:{
		SET_PATH_PAGR(state, newData) {// - 记录当前页 -
			state.currentPage = newData;
		},
		SET_FOLD_EVENT(state, newData) { // 导航栏折叠
			state.eventFold = newData
		},
		SET_USER_INFO(state, newData) { // - 设置用户登录状态 -
			state.userInfo = newData.user
			state.isLoginOut = true
			uni.setStorageSync("uni-id-pages-userInfo", newData.user);
		},
		SET_OUT_LOGIN(state, newData) {// -退出登录-
			state.userInfo = newData.user
			state.isLoginOut = false
			uni.removeStorageSync("uni-id-pages-userInfo"); 
			return setTimeout(() => { uni.reLaunch({ url: '/pages/login/login' }) })
		},
		
		
	},
	actions:{
		async fetchInfoData({ commit }) {
			const USER_INFO = uni.getStorageSync('uni-id-pages-userInfo');
			if (!USER_INFO._id) {
				return setTimeout(() => { commit('SET_OUT_LOGIN', { user: null }); })
			}
			try {
				let loginDate = USER_INFO.last_login_date
				let time = Date.now();
				const expireCycle  = (1000 * 60 * 60 * 24 ) * 7; 
				let isExpire = ((time - loginDate) > expireCycle) ;
				if(isExpire){
					 commit('SET_OUT_LOGIN', { user: null });
				}
			} catch (error) {
				console.error(error);
			}
		},
	}
})

export default store